<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px"
             style="margin-bottom: 20px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name"
                  placeholder="请输入表达式名称"
                  clearable
                  @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="expressionList" @current-change="handleSingleExpSelect">
      <el-table-column width="55" align="center">
        <template #default="scope">
          <!-- 可以手动的修改label的值，从而控制选择哪一项 -->
          <el-radio v-model="radioSelected" :label="scope.row.id">{{ '' }}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="主键" min-width="45rem" align="center" prop="id"/>
      <el-table-column label="名称" min-width="45rem" align="center" prop="name"/>
      <el-table-column label="表达式内容" min-width="45rem" align="center" prop="value"/>
      <el-table-column label="备注" min-width="45rem" align="center" prop="remark"/>
      <el-table-column align="center" label="操作" min-width="45rem" fixed="right">
        <template v-slot="{row}">
          <el-button type="success" link @click="handleView(row)">查看</el-button>
          <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
          <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0"
                :total="total"
                :page-sizes="[5,10]"
                layout="prev, pager, next"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"/>
  </div>
  <el-dialog draggable v-model="dialogVisible" :title="dialogTypeTitle" width="50%" top="2vh">
    <el-form v-if="dialogVisible" class="my-form" ref="obj" :model="obj" :rules="rules" label-width="7rem"
             label-position="right">
      <el-form-item label="表达式名称" prop="name">
        <el-input v-model="obj.name" placeholder="请输入表达式名称，最多输入50个字符"
                  :disabled="dialogType === 'view'"/>
      </el-form-item>
      <el-form-item label="表达式内容" prop="value">
        <el-input v-model="obj.value" placeholder="请输入表达式内容，最多输入200个字符"
                  :disabled="dialogType === 'view'"/>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="obj.remarks" placeholder="请输入备注，最多输入100个字符"
                  :disabled="dialogType === 'view'"/>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-input v-model="obj.createTime" placeholder="自动生成" disabled/>
      </el-form-item>
      <el-form-item label="最后修改时间">
        <el-input v-model="obj.updateTime" placeholder="自动生成" disabled/>
      </el-form-item>
    </el-form>
    <div style="text-align:right;">
      <el-button type="info" icon="Close" @click="dialogVisible=false">关闭</el-button>
      <el-button type="primary" icon="Check" v-show="dialogType !== 'view'" @click="doConfirm('obj')">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {doGet, doPost, doPut, doDelete} from '@/config/http'
import baseConfig from '@/mixins/baseConfig'
import {customValidator, notifySuccess, svc} from "@/config/common";

const emptyData = {
  name: '', // 表达式名称
  value: '', // 表达式内容
  remarks: '', // 备注
  id: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  deleted: null,
  version: null
}

export default {
  name: "FlowExpression",
  mixins: [baseConfig],
  props: {
    // 回显数据传值
    selectValues: {
      type: [Number, String, Array],
      default: null,
      required: false
    },
  },
  data() {
    return {
      expressionList: [],
      open: false,
      loading: true,
      showSearch: true,
      total: 0,
      radioSelected: null,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      dialogType: '',
      dialogTypeTitle: '',
      dialogVisible: false,
      obj: {},
      rules: {
        name: [{
          required: true,
          validator: customValidator,
          min: 1,
          max: 50,
          label: '表达式名称',
          trigger: ['blur', 'change']
        }],
        value: [{
          required: false,
          validator: customValidator,
          min: 0,
          max: 200,
          label: '表达式内容',
          trigger: ['blur', 'change']
        }],
        remarks: [{
          required: false,
          validator: customValidator,
          min: 0,
          max: 100,
          label: '备注',
          trigger: ['blur', 'change']
        }]
      }
    }
  },
  watch: {
    selectValues(newValue) {
      if (newValue) {
        this.radioSelected = newValue
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleEdit(row) {
      this.dialogType = 'edit'
      this.dialogTypeTitle = '编辑'
      this.dialogVisible = true
      this.obj = Object.assign({}, row)
    },
    handleView(row) {
      this.dialogType = 'view'
      this.dialogTypeTitle = '详情'
      this.dialogVisible = true
      this.obj = Object.assign({}, row)
    },
    handleAdd() {
      this.dialogType = 'add'
      this.dialogTypeTitle = '新增'
      this.dialogVisible = true
      this.obj = Object.assign({}, emptyData)
    },
    handleDelete(row) {
      this.$confirm('您确定删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(_ => {
        doDelete(svc, `/bpm-expression/${row.id}`, {}, false).then(() => {
          notifySuccess()
          this.getList()
          this.dialogVisible = false
        })
      })
    },
    async doConfirm(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) {
          return
        }
        if (this.dialogType === 'edit') {
          doPut(svc, '/bpm-expression/modify', this.obj).then(() => {
            notifySuccess()
            this.getList()
            this.dialogVisible = false
          })
        } else if (this.dialogType === 'add') {
          doPost(svc, '/bpm-expression/add', this.obj).then(() => {
            notifySuccess()
            this.getList()
            this.dialogVisible = false
          })
        }
      })
    },
    /** 查询流程达式列表 */
    getList() {
      this.loading = true;
      doGet(svc, '/bpm-expression/page', this.queryParams).then(response => {
        this.expressionList = response?.records;
        this.total = parseInt(response?.total);
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryRef.resetFields()
      this.handleQuery();
    },
    /** 单选框选中数据 */
    handleSingleExpSelect(selection) {
      this.radioSelected = selection.id;//点击当前行时,radio同样有选中效果
      this.$emit('handleSingleExpSelect', selection);
    }
  }
}
</script>

<style scoped>
</style>
